package com.wframe.project.jnd.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wframe.project.jnd.util.TransformUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.frame.database.JdataBean;
import com.frame.database.PageResult;
import com.wframe.project.jnd.rest.util.JsonReturn;
import com.wframe.project.jnd.rest.util.WebSession;
import com.wframe.project.jnd.services.InformationService;
import com.wframe.project.jnd.util.WebUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 新闻资讯广告
 *
 * @author lenovo
 */
@Controller
@RequestMapping("/info")
public class InformationController {

    @Autowired
    InformationService informationService;

//    @RequestMapping("/helps")
//    public String Entrance(){
//        return "/website//user/helps";
//    }
//    @RequestMapping(value = "helplist")
//    @ResponseBody
//    public String Entrance(Integer pno,String like) throws Exception{
//        JsonReturn json = new JsonReturn();
//        Map map = new HashMap();
//        try{
//            JdataBean bean = new JdataBean();
//            int no = 1;
//            if (pno != null) {
//                no = pno.intValue();
//            }
//            PageResult pr = informationService.queryEntrances(bean,14,no);
//            map.put("pno",pno);
//            map.put("list",pr.getList());
//            map.put("pr",WebUtils.toPageBean(pr,7));
//            map.put("like",bean);
//            json.putData(map);
//
//        }catch (Exception e){
//            return json.toErrorString(this.getClass(), e);
//        }
//        return json.toString();
//    }

    /**
     * 帮助中心列表(包含分类和搜索)
     *
     * @param pno
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "helplist")
    public String Entrance(String pno, Model model, String like, String code) throws Exception {
        like = TransformUtils.trans(like);
        code = TransformUtils.trans(code);
        JdataBean bean = new JdataBean();
        JdataBean pamrs = new JdataBean();
        int pagesize = 14;
        int pageno = 1;
        if (StringUtils.isNotBlank(pno)) pageno = Integer.parseInt(pno);
        bean.put("like", like);
        if (StringUtils.isBlank(code)) {
            PageResult pr = informationService.queryEntrances(bean, pagesize, pageno);
            model.addAttribute("list", pr.getList());
            model.addAttribute("pageCount", pr.getPageCount());
            model.addAttribute("pr", WebUtils.toPageBean(pr, 7));

        } else {
            pamrs.put("code", code);
            PageResult bean1 = informationService.getcontentList(pamrs, pagesize, pageno);
            model.addAttribute("helpscotegory", bean1.getList());
            model.addAttribute("pageCount", bean1.getPageCount());

        }

        model.addAttribute("pageno", pageno);
        model.addAttribute("like", like);
        model.addAttribute("chanelCode", code);
        return "/website/user/helps";
    }

    /**
     * 帮助中心文章详情
     */
    @RequestMapping(value = "helpinfo")
    public String helpinfo(String id, Model model) {
        JdataBean bean = informationService.getInfoContentNew(id);
        if (bean != null) {
            model.addAttribute("info", bean.getDataMap());
        }

        model.addAttribute("chanelCode", bean.getString("wicode"));
        return "/website/user/help-sub";
    }

    /**
     * 帮助中心分页
     *
     * @param pno
     * @param like
     * @param code
     * @return
     */
    @RequestMapping(value = "pagination", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String showPagination(String pno, @RequestParam(required = false) String like, @RequestParam(required = false) String code) {
        JdataBean bean = new JdataBean();

        int pagesize = 14;
        int pageno = 1;
        if (StringUtils.isNotBlank(pno)) pageno = Integer.parseInt(pno);
        bean.put("like", like);
        PageResult pr = null;
        if (StringUtils.isBlank(code)) {
            pr = informationService.queryEntrances(bean, pagesize, pageno);
        } else {
            bean.put("code", code);
            pr = informationService.queryInfos(bean, pagesize, pageno);
        }
        return WebUtils.toPageHtml(pr, 7);
    }

    /**
     * 进入信息列表列
     *
     * @param channelcode
     */
    @RequestMapping(value = "showlist")
    public String showlist(String channelcode, Integer pno, Model model) {
        JdataBean bean = informationService.getChannel(channelcode);
        if (bean != null) {
            JdataBean parms = new JdataBean();
            parms.put("code", channelcode);
            model.addAttribute("channeltitle", bean.getString("name"));
            int no = 1;
            if (pno != null) {
                no = pno.intValue();
            }
            PageResult pr = informationService.queryInfos(parms, 20, no);
            model.addAttribute("channelcode", channelcode);
            model.addAttribute("list", pr.getList());
//            model.addAttribute("pagehtml", WebUtils.toPageHtml(pr, 7));
        }
        return "/website/information/infoList";
    }

    /**
     * 加载文章信息列表
     *
     * @param channelcode
     * @param size
     * @param response
     */
    @RequestMapping(value = "list")
    public void list(String channelcode, Integer size, HttpServletResponse response) {
        JsonReturn json = new JsonReturn();
        try {
            int sizeint = 10;
            if (size != null) sizeint = size;
            JdataBean bean = new JdataBean();
            bean.put("code", channelcode);
            json.putData(informationService.queryInfos(bean, sizeint, 1).getList());
            WebUtils.responseWrite(response, json.toString());
        } catch (Exception e) {
            WebUtils.responseWrite(response, json.toErrorString(this.getClass(), e));
        }
    }

    /**
     * 加载文章信息详情
     *
     * @return
     */
    @RequestMapping(value = "info")
    public String info(String id, String code, Model model) {
        JdataBean bean = null;
        if (StringUtils.isNotBlank(id)) {
            bean = informationService.getInfo(id);
        } else if (StringUtils.isNotBlank(code)) {
            bean = informationService.getInfoByCode(code);
        }
        if (bean != null) {
            id = bean.getString("id");
            JdataBean channel = informationService.getChannel(bean.getString("code"));
            bean.put("content", informationService.getInfoCon(id));
            model.addAttribute("info", bean.getDataMap());
            model.addAttribute("channeltitle", channel.getString("name"));
        }
        return "/website/information/info";
    }
}
